import random
import time

import pydirectinput

pydirectinput.PAUSE = None


class PyDirectinputKmHandler:

    def __init__(self):
        self.key_list = []

    def open(self, mouse_speed=None):
        pass

    def close(self):
        pass

    def press_key_by_name(self, key_name):
        key_name = key_name.lower()
        pydirectinput.keyDown(key_name)
        self.key_list.append(key_name)
        return

    def release_key_by_name(self, key_name):
        key_name = key_name.lower()
        pydirectinput.keyUp(key_name)
        if key_name in self.key_list:
            self.key_list.remove(key_name)
        return

    def press_and_release_key_by_name(self, key_name):
        key_name = key_name.lower()
        self.press_key_by_name(key_name)
        delay = random.randint(65, 80)
        time.sleep(delay / 1000)
        self.release_key_by_name(key_name)
        return

    def is_key_pressed_by_name(self, key_name):
        key_name = key_name.lower()
        return key_name in self.key_list

    def release_all_key(self):
        for key_name in self.key_list:
            key_name = key_name.lower()
            pydirectinput.keyUp(key_name)
        self.key_list = []
        return True

    def press_and_release_mouse_button(self, button_name, sleep_time=0.2):
        delay = random.randint(65, 80) / 1000
        if button_name == 1:
            button = pydirectinput.MOUSE_LEFT
        elif button_name == 2:
            button = pydirectinput.MOUSE_MIDDLE
        else:
            button = pydirectinput.MOUSE_RIGHT
        pydirectinput.mouseDown(button=button)
        time.sleep(delay)
        pydirectinput.mouseUp(button=button)
        time.sleep(sleep_time)
        return

    def click(self, sleep_time=0.2):
        delay = random.randint(65, 80) / 1000
        pydirectinput.mouseDown(button=pydirectinput.MOUSE_LEFT)
        time.sleep(delay)
        pydirectinput.mouseUp(button=pydirectinput.MOUSE_LEFT)
        time.sleep(sleep_time)
        return

    def press_mouse_button(self, button_name):
        delay = random.randint(65, 80) / 1000
        if button_name == 1:
            button = pydirectinput.MOUSE_LEFT
        elif button_name == 3:
            # 模拟点击鼠标右键
            button = pydirectinput.MOUSE_RIGHT
        else:
            # 模拟点击鼠标中键
            button = pydirectinput.MOUSE_MIDDLE
        pydirectinput.mouseDown(button=button)
        time.sleep(delay)
        return

    def release_mouse_button(self, button_name):
        delay = random.randint(65, 80) / 1000
        if button_name == 1:
            button = pydirectinput.MOUSE_LEFT
        elif button_name == 3:
            button = pydirectinput.MOUSE_RIGHT
        else:
            button = pydirectinput.MOUSE_MIDDLE
        pydirectinput.mouseUp(button=button)
        time.sleep(delay)
        return

    def release_all_mouse_button(self):
        pydirectinput.mouseUp(button=pydirectinput.MOUSE_LEFT)
        pydirectinput.mouseUp(button=pydirectinput.MOUSE_RIGHT)
        pydirectinput.mouseUp(button=pydirectinput.MOUSE_MIDDLE)
        return True

    def move_mouse_to(self, x, y, sleep_time=0.3):
        x = int(x)
        y = int(y)
        pydirectinput.moveTo(x, y)
        time.sleep(sleep_time)
        return

    def move_mouse_click(self, x, y, sleep_time=0.3):
        self.move_mouse_to(x, y, sleep_time)
        self.press_and_release_mouse_button(1)
        time.sleep(sleep_time)
        return

    def move_mouse_wheel(self, z):
        pydirectinput.scroll(z)
